package com.itheima.controller;


import cn.itheima.health.entity.QueryPageBean;
import cn.itheima.health.entity.Result;
import cn.itheima.health.resources.MessageConstant;
import cn.itheima.health.resources.QiniuUtils;
import cn.itheima.health.resources.RedisConstant;
import cn.itheima.health.resources.UploadUtils;
import cn.itheima.health.service.CheckGroupService;
import cn.itheima.health.service.SetmealService;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Map;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Reference
    private SetmealService setmealService;

    @Autowired
    private JedisPool jedisPool;


    @RequestMapping("/upload.do")
    public Result upload(@RequestParam("imgFile")MultipartFile file){
        try {
            //   七牛云传递文件   uuiid
            String  uuid = UploadUtils.generateRandonFileName(file.getOriginalFilename());
            QiniuUtils.upload2Qiniu(file.getBytes(),uuid); //   文件送给 七牛云
            //  回送uuid 名称
            //  文件 名称 存储 redis
            Jedis jedis = jedisPool.getResource();
            jedis.sadd(RedisConstant.SETMEAL_PIC_RESOURCES,uuid);
            jedis.close();
            return  new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,uuid);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }

    }
    @RequestMapping("/add.do")
    public Result add(@RequestBody Setmeal setmeal, @RequestParam("checkgroupIds") Integer[]checkgroupIds ){
        try {
            setmealService.add(setmeal,checkgroupIds);
            return  new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }

    }



    @RequestMapping("/findPage.do")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {

            Map map = setmealService.findPage(queryPageBean);
            return  new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }

    }

//    @RequestMapping("/findCheckitemsByGroupId.do")
//    public Result findCheckitemsByGroupId(@RequestParam("id") Integer id){
//        try {
//            Map map = checkGroupService.findCheckitemsByGroupId(id);
//            return  new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,map);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return  new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
//        }
//
//    }


//    @RequestMapping("/deleteCheckItemById.do")
//    public Result deleteCheckItemById(@RequestParam("id") Integer id){
//        try {
//            checkItemService.deleteCheckItemById(id);
//            return  new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return  new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
//        }
//
//    }
}
